package com.usian.service.impl;

import com.usian.mapper.TbContentCategoryMapper;
import com.usian.mapper.TbContentMapper;
import com.usian.pojo.TbContentCategory;
import com.usian.pojo.TbContentCategoryExample;
import com.usian.service.ContentCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 韩耀东
 * @date 2021/1/8 8:33
 */
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService {
    @Autowired
    private TbContentMapper contentMapper;

    @Autowired
    private TbContentCategoryMapper contentCategoryMapper;

    /**
     * @return java.util.List<com.usian.pojo.TbContentCategory>
     * @Author 韩耀东
     * @Description //TODO 根据父节点 ID 查询子节点
     * @Date 2021/1/8 8:49
     * @Param [parentId]
     **/
    @Override
    public List<TbContentCategory> selectContentCategoryByParentId(Long parentId) {
        TbContentCategoryExample contentCategoryExample = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = contentCategoryExample.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        return contentCategoryMapper.selectByExample(contentCategoryExample);
    }

    @Override
    public Integer insertContentCategory(TbContentCategory tbContentCategory) {
        tbContentCategory.setUpdated(new Date());
        tbContentCategory.setCreated(new Date());
        tbContentCategory.setIsParent(false);
        tbContentCategory.setSortOrder(1);
        tbContentCategory.setStatus(1);
        Integer contentCategoryNum =
                this.contentCategoryMapper.insert(tbContentCategory);
        //如果他爹不是爹，要把他爹改成爹
        TbContentCategory contentCategory =
                this.contentCategoryMapper.selectByPrimaryKey(tbContentCategory.getParentId());
        if (!contentCategory.getIsParent()) {
            contentCategory.setIsParent(true);
            contentCategory.setUpdated(new Date());
            this.contentCategoryMapper.updateByPrimaryKey(contentCategory);
        }
        return contentCategoryNum;
    }

    @Override
    public Integer deleteContentCategoryById(Long categoryId) {
        TbContentCategory tbContentCategory = contentCategoryMapper.selectByPrimaryKey(categoryId);
        if (tbContentCategory.getIsParent()) {
            //当前节点是父节点，不允许删除
            return 0;
        }
        contentCategoryMapper.deleteByPrimaryKey(categoryId);
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = tbContentCategoryExample.createCriteria();
        criteria.andParentIdEqualTo(tbContentCategory.getParentId());
        //查询当前节点的兄弟节点 删除之后如果当前节点的爹没有儿子了 那它就不是爹了
        List<TbContentCategory> tbContentCategoryList = contentCategoryMapper.selectByExample(tbContentCategoryExample);
        //没有儿子的爹，那就不是爹
        if (tbContentCategoryList.size() == 0) {
            TbContentCategory parenttbContentCategory = new TbContentCategory();
            parenttbContentCategory.setId(tbContentCategory.getParentId());
            //则修改isParent为false
            parenttbContentCategory.setIsParent(false);
            //修改，修改时间
            parenttbContentCategory.setUpdated(new Date());
            contentCategoryMapper.updateByPrimaryKeySelective(parenttbContentCategory);
        }
        return 200;
    }

    @Override
    public Integer updateContentCategory(Long id, String name) {
        TbContentCategory tbContentCategory = contentCategoryMapper.selectByPrimaryKey(id);
        tbContentCategory.setName(name);
        tbContentCategory.setUpdated(new Date());
        return  contentCategoryMapper.updateByPrimaryKey(tbContentCategory);
    }
}
